package com.example.myapplication3

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {
    lateinit var editText: EditText
    val MainTag="MainActivity"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        val editText=findViewById<EditText>(R.id.editTextText)
        val button1=findViewById<Button>(R.id.button1)
        button1.setOnClickListener {
            val intent= Intent(this, MainActivity2::class.java)
            intent.putExtra("name", editText.text.toString())
            startActivity(intent)
        }
        val button2 = findViewById<Button>(R.id.button2)
        val activity2Launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()
        ) { result ->
            if (result.resultCode == RESULT_OK) {
                val name = result.data?.getStringExtra("name")
                editText.setText(name)
            }
            Log.d(MainTag, "activity2Launcher")
        }
        button2.setOnClickListener {
            val intent = Intent(this, MainActivity3::class.java)
            activity2Launcher.launch(intent)
        }

        val button3 = findViewById<Button>(R.id.button3)
        val photoLauncher = registerForActivityResult(
            ActivityResultContracts.TakePicturePreview()) {
            val imageView = findViewById<ImageView>(R.id.imageView)
            imageView.setImageBitmap(it)
        }
        button3.setOnClickListener {
            photoLauncher.launch(null)
        }

        val button4 = findViewById<Button>(R.id.button4)
        button4.setOnClickListener {
            val intent = Intent(this, MainActivity4::class.java)
            startActivity(intent)
        }

        Log.d(MainTag, "onCreate")
    }

    override fun onStart() {
        super.onStart()
        Log.d(MainTag, "onStart")
    }

    override fun onResume() {
        super.onResume()
        Log.d(MainTag, "onResume")
    }

    override fun onPause() {
        super.onPause()
        Log.d(MainTag, "onPause")
    }

    override fun onStop() {
        super.onStop()
        Log.d(MainTag, "onStop")
    }

    override fun onRestart() {
        super.onRestart()
        Log.d(MainTag, "onRestart")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(MainTag, "onDestroy")

    }
}